package com.mall.server.controller;

import com.mall.commons.R;
import com.mall.commons.exceotion.BusinessException;
import com.mall.commons.exceotion.ErrorResult;
import com.mall.server.service.UmsMenuService;
import com.tanhua.model.domain.UmsMenu;
import com.tanhua.model.vo.PageVo;
import com.tanhua.model.vo.UmsMenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/menu")
public class UmsMenuController {

    @Autowired
    private UmsMenuService umsMenuService;

    /**
     * 分页查询后台菜单
     *
     * @param pageNum
     * @param pageSize
     * @param parentId
     * @return
     */
    @GetMapping("/list/{parentId}")
    public R listGetById(Integer pageNum, Integer pageSize, @PathVariable Integer parentId) {

        PageVo vo = umsMenuService.listGetById(pageNum, pageSize, parentId);

        if (vo == null) {
            throw new BusinessException(ErrorResult.emptyError());
        }
        return R.success(vo);
    }

    /**
     * 根据ID获取菜单详情
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R getById(@PathVariable Integer id) {
        UmsMenu umsMenu = umsMenuService.getById(id);
        return R.success(umsMenu);
    }

    /**
     * 修改菜单显示状态
     *
     * @param id
     * @return
     */
    @PostMapping("/updateHidden/{id}")
    public R updateHiddenById(@PathVariable Integer id, Integer hidden) {
        umsMenuService.updateHiddenById(id, hidden);
        return R.success("操作成功");
    }

    /**
     * 根据ID删除后台菜单
     *
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public R deletes(@PathVariable Integer id) {
        umsMenuService.deletes(id);
        return R.success("操作成功");
    }

    /**
     * 添加后台菜单
     *
     * @param umsMenu
     * @return
     */
    @PostMapping("/create")
    public R creates(@RequestBody UmsMenu umsMenu) {
        umsMenuService.creates(umsMenu);
        return R.success("操作成功");
    }

    /**
     * 树形结构返回所有菜单列表
     *
     * @return
     */
    @GetMapping("/treeList")
    public R treeList() {
        List<UmsMenuVo> umsMenu = umsMenuService.treeList();
        return R.success(umsMenu);
    }


    /**
     * 修改后台菜单
     * @param umsMenu
     * @return
     */
    @PostMapping("/update/{id}")
    public R updatess(@RequestBody UmsMenu umsMenu) {
        umsMenuService.updatess(umsMenu);
        return R.success("操作成功");
    }

}
